<?php
include_once dirname(dirname(dirname(__FILE__))).'/include/db_connect.php';
function getAllAnnouncement(){
	$conn = get_db_conn();
	$sql ="select a.*,u.username from announcement a left join user u on a.author_id = u.id ";
	$result = mysql_query($sql,$conn) or die("db error");
	mysql_close($conn);
	return $result;
}
function getContentById($id){
	$conn = get_db_conn();
	$sql ="select content from announcement where id= ".$id;
	$result = mysql_query($sql,$conn) or die("db error");
	$ret = mysql_fetch_array($result);
	mysql_close($conn);
	return $ret['content'];
}
function getTitleById($id){
	$conn = get_db_conn();
	$sql ="select title from announcement where id= ".$id;
	$result = mysql_query($sql,$conn) or die("db error");
	$ret = mysql_fetch_array($result);
	mysql_close($conn);
	return $ret['title'];
}
function saveOrUpdate($content,$user_id,$anId,$title){
	$conn = get_db_conn();
	$date = date("Y-m-d");
	if ($anId=='') {
		$sql = 'insert into announcement(content,date,title,author_id)
		 values('.'"'.$content.'"'.','.'"'.$date.'"'.','.'"'.$title.'"'.','.$user_id.')';
		 if(mysql_query($sql,$conn))return true;
		else  {
		echo $sql;
		echo mysql_error();
		return false;
		}
	}else{

		$sql = 'update announcement set content='.'"'.$content.'"'.','.'date='.'"'.$date.'"'.','
		.'title='.'"'.$title.'"'.','.'author_id='.$user_id.' where id='.$anId;
		 if(mysql_query($sql,$conn))return true;
		else  {
		echo $sql;
		echo mysql_error();
		return false;
		}

	}

}
function deleteById($id){
	$conn = get_db_conn();
	$sql ="delete from announcement where id=".$id.";";
	if(mysql_query($sql,$conn))return true;
	else return false;

}
?>